<?php
/** 
 * Patient_model
 * This class handles model work necessary for Patient Crud and List operations 
 * 
 * @author Ray Nowell
 *	
 */ 
class Patient_model extends MY_Model {

	function Patient_model()
	{
		parent::MY_Model();
	}
	
	function Add_patient($data)
	{
		//Called in upload form so must set created date fields here.
		$data['created_date'] = date('Y-m-d H:i:s');
		$data['created_by'] = $this->ion_auth->get_user()->username;
		return $this->db->insert('patient', $data);
	}
	
	function Update_patient($data)
	{
		$this->db->where('patient_id',element('patient_id', $data));
		$this->db->where('facility_id',$this->facility_id);
		return $this->db->update('patient',$data);
	}
	
	function Delete_patient()
	{
		$ret_val = $this->delete_record('patient','patient_id');
		return $ret_val;	
	}
	/*
	 * Returns a list of current patients
	 */
	function get_patient_list()
	{
		$select_fields = "patient_id
							,patient_id_number
							,patient.last_name
							,first_name";
		//Get the query results for the clinical patients
		$this->db->select($select_fields, false);
		$this->db->from('patient');
		$this->db->where('facility_id',$this->facility_id);
		$this->db->order_by('patient.last_name, patient.first_name');
		$query = $this->db->get();
		 
		return $query->result_array();
	}


	/*
	 * Returns a single patient record based on Patient Id passed 
	 */
	function get_patient_id_record($patient_id)
	{
		$query = $this->db->limit(1)->get_where('patient', array('patient_id' => $patient_id));
		return $query->row();
	}
	
	/*
	 * Returns a single patient record based on Patient Number passed 
	 */
	function get_patient_id_number_record($patient_id_number)
	{
		$query = $this->db->limit(1)->get_where('patient', array('patient_id_number' => $patient_id_number,
																	'facility_id' => $this->facility_id,));
		 
		if ($query->num_rows() > 0)
		 	return $query->row();
		else
			return false;
	}
	
	//Inserts a patient record based on the patient array passed
	//Currently used for demo and only passes the following info:
	//1.	Patient ID Number
	//2.	Last Name
	//3.	First Name
	function insert_patient_record($patient_array)
	{
		$patient_array['created_date'] = date('Y-m-d H:i:s');
		$patient_array['created_by'] = $this->ion_auth->get_user()->username;
		
		
		$patient_id_number = element('patient_id_number', $patient_array);
		
		//Check if patient number already exists
		if ($this->get_patient_id_number_record($patient_id_number) != false)
			return false;
			
		$this->db->insert('patient', $patient_array); 
		
		return true;
	}

	//Inserts a patient record based on the patient array passed
	//Currently used for demo and only passes the following info:
	//1.	Patient ID Number
	//2.	Last Name
	//3.	First Name
	function update_patient_record($patient_array)
	{
		$patient_id_number = element('patient_id_number', $patient_array);

		$this->db->where('patient_id_number', $patient_id_number);
		$this->db->where('facility_id',$this->facility_id);
		$this->db->update('patient', $patient_array);	 	
		
		return true;
		
	}
	
	//Used for Ajax Patient Lookup
	function seek_patients( $patient_match )
		{	
			$this->db->select('patient_id');
			$this->db->select('patient_id_number');
			$this->db->select('CONCAT(`patient_id_number`,CASE length(ifnull(last_name,\'\')) + length(ifnull(first_name,\'\')) WHEN 0 THEN \'\' ELSE concat(\' - \',ifnull(last_name,\'\'),\', \',ifnull(first_name,\'\')) END)	AS full_patient_description', FALSE);
			
			$where	 =	'facility_id = '.$this->facility_id.' AND (';
			$where	.= 'ifnull(patient_id_number,\'\')  LIKE \'%'.$patient_match.'%\' OR ';
			$where	.= 'ifnull(last_name,\'\')  LIKE \'%'.$patient_match.'%\' OR ';
			$where	.= 'ifnull(first_name,\'\')  LIKE \'%'.$patient_match.'%\'';
			$where	.= ')';
			$this->db->where($where);
			$this->db->order_by('patient.last_name, patient.first_name');
			$query=$this->db->get('patient', 5);

			return $query->result();
		}
}
/* End of file patient_model.php */
/* Location: ./system/application/models/patient_model.php */